package com.itheima.mapper;

import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface StudentMapper {

    /**
     * 分页查询
     * @param param
     * @return
     */
    List<Student> page(StudentQueryParam param);

    /**
     * 根据学号查询
     * @param id
     * @return
     */
    @Select("select * from student where id = #{id}")
    Student getByStudentId(Integer id);

    /**
     * 批量删除
     * @param ids
     */
    // @Select("delete from student where id in (#{ids})")
    void deleteBatch(List<Integer> ids);

    /**
     * 添加学员
     * @param student
     */
    //@Select("insert into student(name, no, gender, phone, id_card, is_college, address, degree, graduation_date, clazz_id, create_time, update_time) values (#{name}, #{no}, #{gender}, #{phone}, #{idCard}, #{isCollege}, #{address}, #{degree}, #{graduationDate}, #{clazzId}, #{createTime}, #{updateTime})")
    @Insert("insert into student(name, no, gender, phone,id_card, is_college, address, degree, graduation_date,clazz_id, create_time, update_time) VALUES " +
            "(#{name},#{no},#{gender},#{phone},#{idCard},#{isCollege},#{address},#{degree},#{graduationDate},#{clazzId},#{createTime},#{updateTime})")
    void add(Student student);

    /**
     * 修改学员信息
     * @param student
     */
    void update(Student student);

    /**
     * 修改学员成绩
     * @param id
     * @param score
     * @param updateTime
     */
    @Select("update student set violation_score =  #{score}, update_time = #{updateTime} where id = #{id}")
    void UpdateScore(Integer id, Integer score, LocalDateTime updateTime);

    /**
     * 统计班级人数
     */
    @Select("select c.name cname , count(s.id) scount from clazz c  left join student s on s.clazz_id = c.id group by c.name order by count(s.id) desc ")
    List<Map<String,Object>> getStudentCount();

    /**
     * 统计学员学历
     */
    @MapKey("name")
    List<Map> countStudentDegreeData();

    @Select("select count(*) from student where clazz_id = #{id}")
    Integer countByClazzId(Integer id);
}
